package com.ibeetl.admin.console.service;

import com.ibeetl.admin.console.dao.DictConsoleDao;
import com.ibeetl.admin.console.web.dto.DictExcelImportData;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.service.CoreBaseService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.ExcelError;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.cms.dao.CmsArticleTagDao;
import com.ibeetl.cms.dao.CmsCatalogDictDao;
import com.ibeetl.cms.entity.CmsArticleTag;
import com.ibeetl.cms.entity.CmsCatalogDict;
import org.apache.poi.ss.util.CellReference;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CoreDict Service
 */

@Service
@Transactional
public class DictConsoleService extends CoreBaseService<CoreDict> {

    @Autowired private DictConsoleDao dictDao;
    @Autowired private CmsCatalogDictDao catalogDictConsoleDao;
    @Autowired private CmsArticleTagDao articleTagDao;
    @Autowired private CorePlatformService platformService;

    public PageQuery<CoreDict>queryByCondition(PageQuery query){
        PageQuery ret =  dictDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelCoreDict(List<Long> ids){
        try {
        	//TODO,找到数据字典所有子类，设置删除标记
            dictDao.batchDelCoreDictByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除CoreDict失败", e);
        }
    }
    public List<CoreDict> queryExcel(PageQuery<CoreUser> query) {
        //同查询，不需要额外数据
        PageQuery ret =  dictDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret.getList();
        
    }
    /**
     *  参考：dict_mapping.xml
     * @param list
     * @return
     */
    public void batchInsert(List<DictExcelImportData> list) {
       int dataStartRow = 2;
       final Map<Integer,DictExcelImportData> map = new HashMap<>();
       list.forEach((item)->map.put(item.getExcelId(), item));
       //逐个按照顺序导入
       for(DictExcelImportData item:list) {
           CoreDict dict = new  CoreDict();
           dict.setName(item.getName());
           dict.setRemark(item.getRemark());
           dict.setType(item.getType());
           dict.setTypeName(item.getTypeName());
           dict.setValue(item.getValue());
           
           //设置父字典
           if(item.getParentExcelId()!=0) {
               DictExcelImportData parentItem =  map.get(item.getParentExcelId());
               if(parentItem==null) {
                   //硬编码，TODO,用reader缺少校验，替换手写导入
                   int row = item.getExcelId()+dataStartRow;
                   throwImporError(row,5,"未找到父字典");
               }
               if(parentItem.getId()==null) {
                   int row = item.getExcelId()+dataStartRow;
                   throwImporError(row,5,"父字典未被导入，请先导入父字典");
               }
               dict.setParent(parentItem.getId());
           }
           dict.setCreateTime(new Date());
           //导入前先查找是否有此值
           CoreDict template = new CoreDict();
           template.setType(dict.getType());
           template.setValue(dict.getValue());
           CoreDict dbDict = dictDao.templateOne(template);
           if(dbDict!=null) {
               int row = item.getExcelId()+dataStartRow;
               throwImporError(row,0,"字典数据已经存在");
           }
           dictDao.insert(dict);
           
           item.setId(dict.getId());
           dataStartRow++;
       }
       
       
    }
    
    private void throwImporError(int row,int col,String msg) {
        ExcelError error = new ExcelError();
        CellReference cr = new CellReference(row,col,false,false);
        error.setCell(cr.formatAsString());
        error.setMsg(msg);
        throw new PlatformException("导入错误在:"+error.getCell()+","+msg);
    }
    
    

	/**
	 * 得到栏目对应的所有字典记录
	 * @return
	 */
	public List<Long> getDictByCatalog(Long catalogId){
		return this.catalogDictConsoleDao.getDictIdByCatalog(catalogId);
	}
	
	
	/** 给栏目赋予字典记录
	 * @param adds
	 * @param dels
	 * @return  返回增加的项的id，用于前端
	 */
	public void updateCatalogDict(Long catalogId,List<Long> adds,List<Long> dels){
		for(Long del:dels){
			//获得功能关联的菜单
			CmsCatalogDict temp = new CmsCatalogDict();
			temp.setCatalogId(catalogId);
			temp.setDictId(del);
			CmsCatalogDict catalogDict = catalogDictConsoleDao.templateOne(temp);
			if(catalogDict==null){
				throw new PlatformException("已经被删除了RoleId="+catalogId+" functionId="+del);
			}
			catalogDictConsoleDao.deleteById(catalogDict.getId());
		}
			
		for(Long add:adds){
			CmsCatalogDict catalogDict = new CmsCatalogDict();
			catalogDict.setCreateTime(new Date());
			catalogDict.setCatalogId(catalogId);
			catalogDict.setDictId(add);
			this.sqlManager.insert(catalogDict);
		}
		
		//清楚缓存
		platformService.clearCatalogCache();
			
	}

    /** 给栏目赋予字典记录
     * @param adds
     * @param dels
     * @return  返回增加的项的id，用于前端
     */
    public void updateArticleDict(Long articleId,List<Long> adds,List<Long> dels){
        for(Long del:dels){
            //获得功能关联的菜单
            CmsArticleTag temp = new CmsArticleTag();
            temp.setArticleId(articleId);
            temp.setDictId(del);
            CmsArticleTag articleTag = articleTagDao.templateOne(temp);
            if(articleTag==null){
                throw new PlatformException("已经被删除了RoleId="+articleId+" functionId="+del);
            }
            articleTagDao.deleteById(articleTag.getId());
        }

        for(Long add:adds){
            CmsArticleTag articleTag = new CmsArticleTag();
            articleTag.setCreateTime(new Date());
            articleTag.setArticleId(articleId);
            articleTag.setDictId(add);
            this.sqlManager.insert(articleTag);
        }

        //清楚缓存
        platformService.clearFunctionCache();

    }
}